#pragma once
#include <unistd.h>
#include <pthread.h>

class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&_lock, nullptr);
    }

    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }

    void lock()
    {
        pthread_mutex_lock(&_lock);
    }

    void unlock()
    {
        pthread_mutex_unlock(&_lock);
    }

    pthread_mutex_t* get()
    {
        return &_lock;
    }
private:
    pthread_mutex_t _lock;
};

class MutexGuard
{
public:
    explicit MutexGuard(Mutex& m) : _mutex(m)
    {
        _mutex.lock();
    }
    ~MutexGuard()
    {
        _mutex.unlock();
    }
    // 禁止拷贝和赋值
    MutexGuard(const MutexGuard&) = delete;
    MutexGuard& operator=(const MutexGuard&) = delete;
private:
    Mutex& _mutex;
};
